package cn.schoolwow.util.module.compare.jsonarray.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.jsonarray.QueryJSONArray;
import cn.schoolwow.util.domain.query.jsonobject.QueryJSONObject;
import cn.schoolwow.util.module.compare.jsonarray.domain.CompareJSONArrayQueryOption;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class SetDeleteCompareJSONArrayFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        CompareJSONArrayQueryOption compareJSONArrayQueryOption = (CompareJSONArrayQueryOption) flowContext.checkData("compareJSONArrayQueryOption");

        List afterFieldValueList = QueryJSONArray.newQuery(compareJSONArrayQueryOption.afterJSONArray)
                .execute()
                .getMultipleFieldValueList(compareJSONArrayQueryOption.addCheckFieldNames);

        JSONArray deleteCompareArray = new JSONArray();
        deleteCompareArray.addAll(compareJSONArrayQueryOption.beforeJSONArray);
        deleteCompareArray.removeIf(obj->{
            JSONObject o = (JSONObject) obj;
            Object beforeValue = QueryJSONObject.newQuery(o)
                    .execute()
                    .getMultipleFieldValue(compareJSONArrayQueryOption.addCheckFieldNames);
            if(afterFieldValueList.contains(beforeValue)){
                return true;
            }
            return false;
        });

        flowContext.putTemporaryData("deleteCompareArray", deleteCompareArray);
    }

    @Override
    public String name() {
        return "设置删除JSON列表";
    }
}
